package com.dgut.common.conf;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;

/**
 * 拦截处理
 *
 */
//@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    private ObjectMapper objectMapper;


    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        MappingJackson2HttpMessageConverter converter =new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper);
        return converter;
    }
    @Bean
    ObjectMapper objectMapper(){
        ObjectMapper om =new ObjectMapper();
        om.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss"));
        return  om;
    }

    /**
     *防止@EnableMvc把静态资源覆盖了
     * @param registry
     */
     public  void addResourceHandlers(ResourceHandlerRegistry registry){
        //解决静态资源
         registry.addResourceHandler("/**")
                 .addResourceLocations("classpath:/static/");
         //swagger
         registry.addResourceHandler("/swagger-ui.html")
                 .addResourceLocations("classpath:/META-INF/resources/");
         //swagger无法访问js
         registry.addResourceHandler("/webjars/**")
                 .addResourceLocations("classpath:/META-INF/resources/webjars/");
     }
}
